﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Entities.Users;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;

    public class PromoController
    {
        public decimal calcSalePrice(NB_Store_ModelInfo objMInfo, NB_Store_PromoInfo objPromoInfo)
        {
            decimal unitCost = new decimal();
            if (Conversions.ToDouble(objPromoInfo.PromoAmount) > 0.0)
            {
                unitCost = new decimal(Convert.ToDouble(objMInfo.UnitCost) - Conversions.ToDouble(objPromoInfo.PromoAmount));
            }
            else
            {
                unitCost = objMInfo.UnitCost;
            }
            if (objPromoInfo.PromoPercent != 0)
            {
                unitCost = decimal.Multiply(decimal.Divide(unitCost, 100M), new decimal(100 - objPromoInfo.PromoPercent));
            }
            return unitCost;
        }

        public void ClearCartItemDiscount(int PortalID)
        {
            IEnumerator enumerator;
            CartController controller = new CartController();
            ArrayList currentCartItems = CurrentCart.GetCurrentCartItems(PortalID);
            try
            {
                enumerator = currentCartItems.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    NB_Store_CartItemsInfo current = (NB_Store_CartItemsInfo) enumerator.Current;
                    current.Discount = decimal.Zero;
                    controller.UpdateObjCartItem(current);
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
        }

        public void ClearSaleRates(int PortalID, int ModelID)
        {
            DataProvider.Instance().ClearNB_Store_SaleRates(PortalID, ModelID);
        }

        public void createSalePriceCache(int PortalID, int ModelID)
        {
            IEnumerator enumerator;
            ArrayList saleRatesList = this.GetSaleRatesList(PortalID, ModelID);
            try
            {
                enumerator = saleRatesList.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    NB_Store_SaleRatesInfo current = (NB_Store_SaleRatesInfo) enumerator.Current;
                    DataCache.SetCache(current.CacheKey, current.SalePrice, DateAndTime.DateAdd(DateInterval.Day, 1.0, DateAndTime.Now));
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            if (ModelID == -1)
            {
                DataCache.SetCache("SalePrice" + Conversions.ToString(PortalID), "TRUE", DateAndTime.DateAdd(DateInterval.Day, 1.0, DateAndTime.Now));
            }
        }

        public void createSalePriceTable(int PortalID, int ModelID = -1)
        {
            ArrayList list2;
            NB_Store_ModelInfo model;
            IEnumerator enumerator;
            IEnumerator enumerator2;
            ProductController controller = new ProductController();
            SettingsController controller2 = new SettingsController();
            string str = "";
            Hashtable hashtable = new Hashtable();
            ArrayList saleRatesList = this.GetSaleRatesList(PortalID, ModelID);
            try
            {
                enumerator = saleRatesList.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    NB_Store_SaleRatesInfo current = (NB_Store_SaleRatesInfo) enumerator.Current;
                    DataCache.RemoveCache(current.CacheKey);
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            this.ClearSaleRates(PortalID, ModelID);
            if (ModelID == -1)
            {
                list2 = controller.GetModelStockList(PortalID, "", SharedFunctions.GetCurrentCulture(), -1, 1, 0x1869f, true);
            }
            else
            {
                model = controller.GetModel(ModelID, SharedFunctions.GetCurrentCulture());
                list2 = new ArrayList();
                list2.Add(model);
            }
            try
            {
                enumerator2 = list2.GetEnumerator();
                while (enumerator2.MoveNext())
                {
                    IEnumerator enumerator3;
                    model = (NB_Store_ModelInfo) enumerator2.Current;
                    SharedFunctions.ClearProductCache(PortalID, model.ProductID);
                    saleRatesList = this.GetPromoList(model.PortalID, "STO", "", true);
                    try
                    {
                        enumerator3 = saleRatesList.GetEnumerator();
                        while (enumerator3.MoveNext())
                        {
                            NB_Store_PromoInfo objPromoInfo = (NB_Store_PromoInfo) enumerator3.Current;
                            str = "SalePrice" + Conversions.ToString(objPromoInfo.PortalID) + "*" + objPromoInfo.PromoGroup + "*" + Conversions.ToString(objPromoInfo.ObjectID) + "*" + Conversions.ToString(model.ModelID);
                            NB_Store_SaleRatesInfo objInfo = new NB_Store_SaleRatesInfo {
                                CacheKey = str,
                                CategoryID = objPromoInfo.ObjectID,
                                ItemID = -1,
                                ModelID = model.ModelID,
                                PortalID = model.PortalID,
                                RoleName = objPromoInfo.PromoGroup,
                                SalePrice = this.calcSalePrice(model, objPromoInfo)
                            };
                            this.UpdateObjSaleRates(objInfo);
                            if ((objInfo.RoleName != "") && !hashtable.ContainsValue(objInfo.RoleName))
                            {
                                hashtable.Add(objInfo.RoleName, objInfo.RoleName);
                            }
                        }
                    }
                    finally
                    {
                        if (enumerator3 is IDisposable)
                        {
                            (enumerator3 as IDisposable).Dispose();
                        }
                    }
                }
            }
            finally
            {
                if (enumerator2 is IDisposable)
                {
                    (enumerator2 as IDisposable).Dispose();
                }
            }
            this.createSalePriceCache(PortalID, ModelID);
        }

        public void DeletePromo(int PromoID)
        {
            DataProvider.Instance().DeleteNB_Store_Promo(PromoID);
        }

        public void DeleteSaleRates(int ItemID)
        {
            DataProvider.Instance().DeleteNB_Store_SaleRates(ItemID);
        }

        public decimal getCartLevelDiscount(int PortalID, UserInfo UserInfo, bool AllowMultiDis)
        {
            IEnumerator enumerator;
            IEnumerator enumerator2;
            decimal num2 = new decimal();
            int num4 = 0;
            decimal num3 = new decimal();
            bool flag = false;
            ArrayList currentCartItems = CurrentCart.GetCurrentCartItems(PortalID);
            try
            {
                enumerator = currentCartItems.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    NB_Store_CartItemsInfo current = (NB_Store_CartItemsInfo) enumerator.Current;
                    if (AllowMultiDis)
                    {
                        num3 = decimal.Add(num3, decimal.Add(decimal.Multiply(new decimal(current.Quantity), current.UnitCost), decimal.Multiply(new decimal(current.Quantity), current.Discount)));
                    }
                    else
                    {
                        num3 = decimal.Add(num3, decimal.Multiply(new decimal(current.Quantity), current.UnitCost));
                    }
                    num4 += current.Quantity;
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            currentCartItems = this.GetPromoList(PortalID, "CAR", "", true);
            try
            {
                enumerator2 = currentCartItems.GetEnumerator();
                while (enumerator2.MoveNext())
                {
                    string[] strArray;
                    NB_Store_PromoInfo info2 = (NB_Store_PromoInfo) enumerator2.Current;
                    flag = true;
                    if (((num4 < info2.QtyRange1) | (num4 > info2.QtyRange2)) & !((info2.QtyRange1 == 0) & (info2.QtyRange2 == 0)))
                    {
                        flag = false;
                    }
                    if ((Convert.ToDouble(num3) < Conversions.ToDouble(info2.Range1)) | ((Convert.ToDouble(num3) > Conversions.ToDouble(info2.Range2)) & !((Conversions.ToDouble(info2.Range1) == 0.0) & (Conversions.ToDouble(info2.Range2) == 0.0))))
                    {
                        flag = false;
                    }
                    if (!UserInfo.IsInRole(info2.PromoGroup) & (info2.PromoGroup != ""))
                    {
                        flag = false;
                    }
                    if (info2.PromoEmail != "")
                    {
                        flag = false;
                        strArray = info2.PromoEmail.Split(new char[] { ',' });
                        int upperBound = strArray.GetUpperBound(0);
                        for (int i = 0; i <= upperBound; i++)
                        {
                            if (strArray[i] == UserInfo.Email)
                            {
                                flag = true;
                            }
                        }
                    }
                    if (info2.PromoUser != "")
                    {
                        flag = false;
                        strArray = info2.PromoUser.Split(new char[] { ',' });
                        int num9 = strArray.GetUpperBound(0);
                        for (int j = 0; j <= num9; j++)
                        {
                            if (strArray[j] == UserInfo.Username)
                            {
                                flag = true;
                            }
                        }
                    }
                    if (flag)
                    {
                        decimal num7 = new decimal();
                        if (Conversions.ToDouble(info2.PromoAmount) > 0.0)
                        {
                            num7 = new decimal(Convert.ToDouble(num3) - Conversions.ToDouble(info2.PromoAmount));
                        }
                        else
                        {
                            num7 = num3;
                        }
                        if (info2.PromoPercent != 0)
                        {
                            num7 = decimal.Multiply(decimal.Divide(num7, 100M), new decimal(100 - info2.PromoPercent));
                        }
                        if (AllowMultiDis)
                        {
                            num2 = decimal.Add(num2, decimal.Subtract(num7, num3));
                        }
                        else if (decimal.Compare(decimal.Subtract(num7, num3), num2) < 0)
                        {
                            num2 = decimal.Subtract(num7, num3);
                        }
                    }
                }
            }
            finally
            {
                if (enumerator2 is IDisposable)
                {
                    (enumerator2 as IDisposable).Dispose();
                }
            }
            if (decimal.Compare(num2, decimal.Zero) > 0)
            {
                num2 = new decimal();
            }
            return num2;
        }

        public ArrayList getCouponCartItems(int PortalID, string CouponCode)
        {
            return this.getCouponCartItems(PortalID, CouponCode, true);
        }

        public ArrayList getCouponCartItems(int PortalID, string CouponCode, bool IsInCategory)
        {
            IEnumerator enumerator;
            ArrayList list = new ArrayList();
            ArrayList list2 = this.GetPromoList(PortalID, "COU", CouponCode, true);
            try
            {
                enumerator = list2.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    NB_Store_PromoInfo current = (NB_Store_PromoInfo) enumerator.Current;
                    if (current.PromoCode == CouponCode)
                    {
                        ProductController controller = new ProductController();
                        ArrayList currentCartItems = CurrentCart.GetCurrentCartItems(PortalID);
                        if (current.ObjectID == -1)
                        {
                            list = currentCartItems;
                        }
                        else
                        {
                            IEnumerator enumerator2;
                            try
                            {
                                enumerator2 = currentCartItems.GetEnumerator();
                                while (enumerator2.MoveNext())
                                {
                                    NB_Store_CartItemsInfo info = (NB_Store_CartItemsInfo) enumerator2.Current;
                                    NB_Store_ModelInfo model = controller.GetModel(info.ModelID, SharedFunctions.GetCurrentCulture());
                                    if (controller.IsInCategory(model.ProductID, Conversions.ToString(current.ObjectID)) == IsInCategory)
                                    {
                                        list.Add(info);
                                    }
                                }
                            }
                            finally
                            {
                                if (enumerator2 is IDisposable)
                                {
                                    (enumerator2 as IDisposable).Dispose();
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            return list;
        }

        public decimal getCouponLevelDiscount(int PortalID, UserInfo UserInfo, string CouponCode)
        {
            IEnumerator enumerator;
            IEnumerator enumerator2;
            decimal num2 = new decimal();
            int num4 = 0;
            decimal num3 = new decimal();
            bool flag = false;
            ArrayList list = this.getCouponCartItems(PortalID, CouponCode);
            try
            {
                enumerator = list.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    NB_Store_CartItemsInfo current = (NB_Store_CartItemsInfo) enumerator.Current;
                    num3 = decimal.Add(num3, decimal.Add(decimal.Multiply(new decimal(current.Quantity), current.UnitCost), decimal.Multiply(new decimal(current.Quantity), current.Discount)));
                    num4 += current.Quantity;
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            ArrayList list2 = this.GetPromoList(PortalID, "COU", CouponCode, true);
            try
            {
                enumerator2 = list2.GetEnumerator();
                while (enumerator2.MoveNext())
                {
                    string[] strArray;
                    NB_Store_PromoInfo info2 = (NB_Store_PromoInfo) enumerator2.Current;
                    flag = true;
                    if (((num4 < info2.QtyRange1) | (num4 > info2.QtyRange2)) & !((info2.QtyRange1 == 0) & (info2.QtyRange2 == 0)))
                    {
                        flag = false;
                    }
                    if ((Convert.ToDouble(num3) < Conversions.ToDouble(info2.Range1)) | ((Convert.ToDouble(num3) > Conversions.ToDouble(info2.Range2)) & !((Conversions.ToDouble(info2.Range1) == 0.0) & (Conversions.ToDouble(info2.Range2) == 0.0))))
                    {
                        flag = false;
                    }
                    if (!UserInfo.IsInRole(info2.PromoGroup) & (info2.PromoGroup != ""))
                    {
                        flag = false;
                    }
                    if (info2.PromoEmail != "")
                    {
                        flag = false;
                        strArray = info2.PromoEmail.Split(new char[] { ',' });
                        int upperBound = strArray.GetUpperBound(0);
                        for (int i = 0; i <= upperBound; i++)
                        {
                            if (strArray[i] == UserInfo.Email)
                            {
                                flag = true;
                            }
                        }
                    }
                    if (info2.PromoUser != "")
                    {
                        flag = false;
                        strArray = info2.PromoUser.Split(new char[] { ',' });
                        int num11 = strArray.GetUpperBound(0);
                        for (int j = 0; j <= num11; j++)
                        {
                            if (strArray[j] == UserInfo.Username)
                            {
                                flag = true;
                            }
                        }
                    }
                    if ((info2.PromoCode == CouponCode) & flag)
                    {
                        flag = true;
                    }
                    else
                    {
                        flag = false;
                    }
                    if ((((info2.PromoCode != "") && (info2.PromoCode == CouponCode)) && (info2.MaxUsage > 0)) && (this.GetPromoCodeUsage(PortalID, CouponCode) >= info2.MaxUsage))
                    {
                        flag = false;
                    }
                    if (((info2.PromoCode != "") && (info2.PromoCode == CouponCode)) && (info2.MaxUsagePerUser > 0))
                    {
                        IEnumerator enumerator3;
                        ArrayList orderList = new OrderController().GetOrderList(PortalID, UserInfo.UserID);
                        int num8 = 0;
                        try
                        {
                            enumerator3 = orderList.GetEnumerator();
                            while (enumerator3.MoveNext())
                            {
                                NB_Store_OrdersInfo info3 = (NB_Store_OrdersInfo) enumerator3.Current;
                                if ((info3.PromoCode == info2.PromoCode) & info3.OrderIsPlaced)
                                {
                                    num8++;
                                }
                            }
                        }
                        finally
                        {
                            if (enumerator3 is IDisposable)
                            {
                                (enumerator3 as IDisposable).Dispose();
                            }
                        }
                        if (num8 >= info2.MaxUsagePerUser)
                        {
                            flag = false;
                        }
                    }
                    if (flag)
                    {
                        decimal num9 = new decimal();
                        if (Conversions.ToDouble(info2.PromoAmount) > 0.0)
                        {
                            num9 = new decimal(Convert.ToDouble(num3) - Conversions.ToDouble(info2.PromoAmount));
                        }
                        else
                        {
                            num9 = num3;
                        }
                        if (info2.PromoPercent != 0)
                        {
                            num9 = decimal.Multiply(decimal.Divide(num9, 100M), new decimal(100 - info2.PromoPercent));
                        }
                        num2 = decimal.Add(num2, decimal.Subtract(num9, num3));
                    }
                }
            }
            finally
            {
                if (enumerator2 is IDisposable)
                {
                    (enumerator2 as IDisposable).Dispose();
                }
            }
            if (decimal.Compare(num2, decimal.Zero) > 0)
            {
                num2 = new decimal();
            }
            return num2;
        }

        public NB_Store_PromoInfo GetPromo(int PromoID)
        {
            return (NB_Store_PromoInfo) CBO.FillObject(DataProvider.Instance().GetNB_Store_Promo(PromoID), typeof(NB_Store_PromoInfo));
        }

        public int GetPromoCodeUsage(int PortalID, string PromoCode)
        {
            return DataProvider.Instance().GetNB_Store_PromoCodeUsage(PortalID, PromoCode);
        }

        public ArrayList GetPromoList(int PortalID, string PromoType, string SearchText, bool GetActiveOnly = false)
        {
            return CBO.FillCollection(DataProvider.Instance().GetNB_Store_PromoList(PortalID, PromoType, SearchText, GetActiveOnly), typeof(NB_Store_PromoInfo));
        }

        public decimal GetSalePrice(NB_Store_ModelInfo objMInfo, UserInfo UserInfo)
        {
            IEnumerator enumerator;
            ProductController controller = new ProductController();
            double num2 = -1.0;
            double num3 = 0.0;
            ArrayList categoriesAssigned = controller.GetCategoriesAssigned(objMInfo.ProductID);
            try
            {
                enumerator = categoriesAssigned.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    NB_Store_ProductCategoryInfo current = (NB_Store_ProductCategoryInfo) enumerator.Current;
                    num3 = Convert.ToDouble(this.GetSalePrice(objMInfo, UserInfo, current.CategoryID));
                    if ((num3 >= 0.0) & (num3 < num2))
                    {
                        num2 = num3;
                    }
                    else if (num2 == -1.0)
                    {
                        num2 = num3;
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            return new decimal(num2);
        }

        public decimal GetSalePrice(NB_Store_ModelInfo objMInfo, UserInfo UserInfo, int CategoryId)
        {
            string[] strArray;
            SettingsController controller = new SettingsController();
            double num2 = -1.0;
            NB_Store_SettingsInfo info = controller.GetSetting(objMInfo.PortalID, "promo.roles", "None");
            if (info != null)
            {
                strArray = ("," + info.SettingValue).Split(new char[] { ',' });
            }
            else
            {
                strArray = Strings.Split("", ",", -1, CompareMethod.Binary);
            }
            if (DataCache.GetCache("SalePrice" + objMInfo.PortalID.ToString()) == null)
            {
                this.createSalePriceCache(objMInfo.PortalID, -1);
            }
            int upperBound = strArray.GetUpperBound(0);
            for (int i = 0; i <= upperBound; i++)
            {
                if (UserInfo.IsInRole(strArray[i]) | (strArray[i] == ""))
                {
                    string cacheKey = "SalePrice" + Conversions.ToString(objMInfo.PortalID) + "*" + strArray[i] + "*" + Conversions.ToString(CategoryId) + "*" + Conversions.ToString(objMInfo.ModelID);
                    if ((DataCache.GetCache(cacheKey) != null) && Versioned.IsNumeric(RuntimeHelpers.GetObjectValue(DataCache.GetCache(cacheKey))))
                    {
                        if (Conversions.ToDouble(DataCache.GetCache(cacheKey)) < num2)
                        {
                            num2 = Conversions.ToDouble(DataCache.GetCache(cacheKey));
                        }
                        else if (num2 == -1.0)
                        {
                            num2 = Conversions.ToDouble(DataCache.GetCache(cacheKey));
                        }
                    }
                    if (CategoryId > -1)
                    {
                        cacheKey = "SalePrice" + Conversions.ToString(objMInfo.PortalID) + "*" + strArray[i] + "*-1*" + Conversions.ToString(objMInfo.ModelID);
                        if ((DataCache.GetCache(cacheKey) != null) && Versioned.IsNumeric(RuntimeHelpers.GetObjectValue(DataCache.GetCache(cacheKey))))
                        {
                            if (Conversions.ToDouble(DataCache.GetCache(cacheKey)) < num2)
                            {
                                num2 = Conversions.ToDouble(DataCache.GetCache(cacheKey));
                            }
                            else if (num2 == -1.0)
                            {
                                num2 = Conversions.ToDouble(DataCache.GetCache(cacheKey));
                            }
                        }
                    }
                }
            }
            return new decimal(num2);
        }

        public NB_Store_SaleRatesInfo GetSaleRates(int ItemID)
        {
            return (NB_Store_SaleRatesInfo) CBO.FillObject(DataProvider.Instance().GetNB_Store_SaleRates(ItemID), typeof(NB_Store_SaleRatesInfo));
        }

        public NB_Store_SaleRatesInfo GetSaleRatesByCacheKey(string CacheKey)
        {
            return (NB_Store_SaleRatesInfo) CBO.FillObject(DataProvider.Instance().GetNB_Store_SaleRatesByCacheKey(CacheKey), typeof(NB_Store_SaleRatesInfo));
        }

        public ArrayList GetSaleRatesList(int PortalID, int ModelID)
        {
            return CBO.FillCollection(DataProvider.Instance().GetNB_Store_SaleRatesList(PortalID, ModelID), typeof(NB_Store_SaleRatesInfo));
        }

        public void UpdateObjPromo(NB_Store_PromoInfo objInfo)
        {
            DataProvider.Instance().UpdateNB_Store_Promo(objInfo.PromoID, objInfo.PortalID, objInfo.ObjectID, objInfo.PromoName, objInfo.PromoType, Conversions.ToDecimal(objInfo.Range1), Conversions.ToDecimal(objInfo.Range2), objInfo.RangeStartDate, objInfo.RangeEndDate, objInfo.PromoAmount, objInfo.PromoPercent, objInfo.Disabled, objInfo.PromoCode, objInfo.PromoGroup, objInfo.PromoUser, objInfo.QtyRange1, objInfo.QtyRange2, objInfo.PromoEmail, objInfo.XMLData, objInfo.MaxUsagePerUser, objInfo.MaxUsage);
        }

        public void UpdateObjSaleRates(NB_Store_SaleRatesInfo objInfo)
        {
            DataProvider.Instance().UpdateNB_Store_SaleRates(objInfo.ItemID, objInfo.CacheKey, objInfo.PortalID, objInfo.RoleName, objInfo.CategoryID, objInfo.ModelID, Conversions.ToString(objInfo.SalePrice));
        }
    }
}

